using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameLib.GameItem
{
    class GameButton : GameItem
    {
        SpriteFont textFont;
        String buttonText;
        Vector2 textPos;

        public GameButton(Texture2D texture,  Vector2 pos, String text, SpriteFont font, ItemManager man)
            : base(texture, man, pos)
        {
            if (font != null)
            {
                buttonText = text;
                textFont = font;
                Vector2 textSize = textFont.MeasureString(text);
                textPos.X = Position.X + (texture.Width - textSize.X) / 2;
                textPos.Y = Position.Y + (texture.Height - textSize.Y) / 2;
            }
        }
        
        public override bool CheckPress(Vector2 touchPos)
        {
            if (Enable)
            {
                if (touchPos.X < Position.X || touchPos.Y < Position.Y
                  || touchPos.X > ItemBoundRightPos || touchPos.Y > ItemBoundDownPos)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        public override void Clean()
        {
            base.Clean();
            textFont = null;
            buttonText = null;
        }

        public override void Update(GameTime gametime)
        {
            base.Update(gametime);
        }

        public override void Draw(SpriteBatch sp)
        {
            base.Draw(sp);
            if (textFont != null)
            {
                if (Trans != Transition.None)
                {
                    if (Enable)
                    {
                        sp.DrawString(textFont, buttonText, textPos, Color.White * Alpha);
                    }
                    else
                    {
                        if (Alpha > 0.5f)
                        {
                            sp.DrawString(textFont, buttonText, textPos, Color.White * 0.5f);
                        }
                        else
                        {
                            sp.DrawString(textFont, buttonText, textPos, Color.White * Alpha);
                        }
                    }
                }
                else
                {
                    if (Enable)
                    {
                        sp.DrawString(textFont, buttonText, textPos, Color.White);
                    }
                    else
                    {
                        sp.DrawString(textFont, buttonText, textPos, Color.White * 0.5f);
                    }
                }
            }
        }
    }
}
